<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-button type="primary" @click="openDialog()">生成订单</el-button>
        </el-col>
        <el-col :span="4">
          <el-button type="success" @click="postOrder()">确认订单</el-button>
        </el-col>
      </el-row>

      <!-- 零件列表区域 -->
      <el-table :data="orderForm.bestlist" border stripe>
        <el-table-column type="index"></el-table-column>

        <el-table-column label="零件名" prop="cname"></el-table-column>
        <el-table-column label="供应商" prop="sname"></el-table-column>
        <el-table-column label="单价" prop="price"></el-table-column>
        <el-table-column label="数量" prop="num"></el-table-column>
        <el-table-column label="总价" prop="t_price"></el-table-column>
        <el-table-column label="操作" width="160px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.cname)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeCompByName(scope.row.cname)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-row :gutter="20">
        <el-col :span="4">
          <span>订单总额：{{orderForm.best_price}}</span>
        </el-col>
      </el-row>
    </el-card>

    <!-- 生成订单的对话框 -->

    <el-dialog title="生成订单" :visible.sync="addDialogVisible" width="30%" @close="addDialogClosed">
      <!-- 内容主体区域 -->
      <el-form :model="queryInfo" :rules="editFormRules" ref="queryFormRef" label-width="100px">
        <el-form-item
          :label="Item.cname"
          v-for="Item in queryInfo"
          :key="Item.cname"
          :value="Item.cname"
        >
          <el-input-number
            v-model="Item.num"
            @change="handleChange"
            :min="0"
            :max="1000"
            label="数量"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="getOrders()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改零件的对话框 -->
    <el-dialog
      title="修改零件供应"
      :visible.sync="editDialogVisible"
      width="30%"
      @close="editDialogClosed"
    >
      <el-form :model="otherData" :rules="editFormRules" ref="otherDataRef" label-width="70px">
        <el-form-item label="零件名">
          <el-input v-model="otherData.cname" disabled></el-input>
        </el-form-item>
        <el-form-item label="供应商">
          <el-select v-model="otherData.sname" style="width:100%">
            <el-option
              v-for="item in otherData.comps"
              :key="item.cid"
              :label="item.sname"
              :value="item.sname"
            >
              {{item.sname}}--单价:{{item.price}}
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editOrderInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
   

    return {
      //订单查询参数
      queryInfo: [],
      orderForm: [],
      otherData: [],

      
      
      // 控制添加零件对话框的显示与隐藏
      addDialogVisible: false,
      // 添加零件的表单数据
      addForm: {
        cname: '',
        sid: '',
        sname: '',
        price: ''
      },

      // 添加表单的验证规则对象
      addFormRules: {
        
      },
      // 控制修改用户对话框的显示与隐藏
      editDialogVisible: false,
      // 查询到的用户信息对象
      editForm: {},
      // 修改表单的验证规则对象
      editFormRules: {
        
      }
    }
  },
  created() {
    
  },
  methods: {
    // 根据cname删除订单对应的零件信息
    async removeCompByName(cname) {
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该零件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }

      const { data: res } = await this.$http.post('order/deleteComp', {
        cname:cname,'data':this.orderForm
      })

      if (res.meta.status !== 200) {
        return this.$message.error('删除零件失败！')
      }

      this.$message.success('删除零件成功！')
      this.orderForm = res.data
      
    },
    // 展示编辑零件的对话框
    async showEditDialog(cname) {
      const { data: res } = await this.$http.get('comp/getCompByName', {
        params: { cname: cname }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('查询零件信息失败！')
      }
      console.log(res)
      this.otherData = res.data
      this.editDialogVisible = true
    },
    // 监听修改用户对话框的关闭事件
    editDialogClosed() {
      this.$refs.otherDataRef.resetFields()
    },
    // 修改订单信息并提交
    editOrderInfo() {
      this.$refs.otherDataRef.validate(async valid => {
        if (!valid) return
        // 发起修改用户信息的数据请求
        const { data: res } = await this.$http.post(
          'order/updateOrder',
          {'update':this.otherData,'data':this.orderForm}
        )
        console.log(res)
        if (res.meta.status !== 200) {
          return this.$message.error('更新零件供应商失败！')
        }
        // 关闭对话框
        this.editDialogVisible = false
        // 刷新数据列表
        this.orderForm = res.data
        // 提示修改成功
        this.$message.success('更新零件供应商成功！')
      })
    },

    async getCnames() {
      //const { data: res } = await this.$http.get('comp/listComp', {
      const { data: res } = await this.$http.get('comp/listCompNames')
      if (res.meta.status == 200) {
        this.queryInfo = res.data.cnames
        return this.$message.success('获取零件列表！')
      } else {
        return this.$message.error('获取零件列表失败！')
      }
      console.log(res)
    },
    // 监听订购对话框的关闭事件
    addDialogClosed() {
      //this.$refs.addFormRef.resetFields()
    },
    //打开订购对话框
    openDialog() {
    
      this.getCnames()
      this.addDialogVisible = true
    },
    //得到推荐的订单
    getOrders() {
      this.$refs.queryFormRef.validate(async valid => {
        if (!valid) return
        // 可以发起添加用户的网络请求
        const { data: res } = await this.$http.post('order/getOrder', {
          data: this.queryInfo
        })

        if (res.meta.status !== 200) {
          return this.$message.error('生成订单失败！')
        }
        this.orderForm = res.best
        //this.otherData = res.data
        this.$message.success('成功生成推荐订单！')
        // 隐藏添加用户的对话框
        this.addDialogVisible = false
      })
    },
    postOrder() {
      const confirmResult = this.$confirm('是否确认发出订单申请', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.orderForm = []
          this.$message({
            type: 'success',
            message: '订单申请成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    }

    

   
  }
}
</script>

<style lang="less" scoped>
</style>
